//
//  NRWCarListToShareVC.m
//  New_Runway
//
//  Created by 张记 on 2018/5/29.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "NRWCarListToShareVC.h"
#import "LYSideslipCell.h"
#import "NRWCarListsModel.h"
#import "NRWCanChooseCarLisetCell.h"
#import "NRWCarShareListModel.h"
#import <SDCycleScrollView.h>
#import "UIImage+ImageCompress.h"
#import "NRWCarShareListShopCell.h"
#import "NRWCarShareBottomView.h"
#import "NRWCarShareTool.h"

@interface NRWCarListToShareVC ()<UITableViewDelegate,
UITableViewDataSource,
UIGestureRecognizerDelegate,
LYSideslipCellDelegate,
UITextFieldDelegate,SDCycleScrollViewDelegate>
@property(nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *AllCarIds;
@property (nonatomic, assign) NSInteger pageNo;
@property (nonatomic, strong) NSMutableArray *carLists;
@property (nonatomic,strong) SDCycleScrollView *bannerView;
@property (nonatomic,strong) NRWCarShareListModel *shareModel;
@property (strong, nonatomic) NRWCarShareBottomView *shareBottomView;

@end

@implementation NRWCarListToShareVC
-(NSMutableArray *)AllCarIds{
    if (!_AllCarIds) {
        _AllCarIds = [[NSMutableArray alloc] init];
    }
    return _AllCarIds;
}
-(NSMutableArray *)carLists{
    if (!_carLists) {
        _carLists = [[NSMutableArray alloc] init];
    }
    return _carLists;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationBarView.rightItem setTitle:@"去分享" forState:UIControlStateNormal];
    self.view.backgroundColor = kColorWhite();
    for (Result *rt in self.dataArray) {
        [self.AllCarIds addObject:rt.carId];
    }
    self.pageNo = 1;
    [self loadCarShareList];
    [self initTableview];
    
    
    
}
-(void)initTableview{
    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    _tableview.estimatedRowHeight = 0;
    _tableview.estimatedSectionHeaderHeight = 0;
    _tableview.estimatedSectionFooterHeight = 0;
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = false;
    }
    self.tableview.backgroundColor = kColorLightgrayBackground();
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerNib:[UINib nibWithNibName:@"NRWCanChooseCarLisetCell" bundle:nil] forCellReuseIdentifier:@"NRWCanChooseCarLisetCell"];
    [self.tableview registerNib:[UINib nibWithNibName:@"NRWCarShareListShopCell" bundle:nil] forCellReuseIdentifier:@"NRWCarShareListShopCell"];
//    self.tableview.mj_footer=[MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upPullLoadMore)];
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).mas_equalTo(self.navH);
        make.bottom.equalTo(weakSelf.view);
        make.left.right.equalTo(weakSelf.view);
    }];
    self.bannerView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0,Screen_width, Screen_width * 6 / 25) delegate:self placeholderImage:[UIImage imageNamed:@"banner"]];
    self.bannerView.currentPageDotImage = [UIImage imageWithColor:MAIN_NAV_COLOR() forSize:CGSizeMake(15, 2)];
    self.bannerView.pageDotImage = [UIImage imageWithColor:[UIColor whiteColor] forSize:CGSizeMake(15, 2)];
    self.bannerView.localizationImageNamesGroup = @[@"share_banner"];
    self.tableview.tableHeaderView = self.bannerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 110;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.carLists.count + 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0) {
        NRWCarShareListShopCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NRWCarShareListShopCell"];
        cell.shopModel = self.shareModel;
        return cell;
    }else{
        ShareCars *carModel = self.carLists[indexPath.row - 1];
        NRWCanChooseCarLisetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NRWCanChooseCarLisetCell"];
        cell.shareCarModel = carModel;
        cell.leftDis.constant = 16;
        cell.selectedImage.hidden = YES;
        return cell;
        
    }
  
}

-(void)upPullLoadMore{
    self.pageNo += 1;
    [self loadCarShareList];
}
-(void)loadCarShareList{
    [SVProgressHUD show];
    NSString *url = [NSString stringWithFormat:@"%@%@",Api,ShareListApi];
    NSDictionary *params = @{
                             @"carIds":[self.AllCarIds componentsJoinedByString:@","],
                             @"shopId":[NRWCarUser sharedUser].shopId ? [NRWCarUser sharedUser].shopId : @"",
                             @"userId":[NRWCarUser sharedUser].userId,

                             };
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:params progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
   
    } successBlock:^(id response) {
        NRWCarShareListModel *model = [NRWCarShareListModel mj_objectWithKeyValues:response];
        self.shareModel = model;
        if (model.code == 1) {
            for (ShareCars *car in model.result.cars) {
                [self.carLists addObject:car];
            }
        }
        self.navigationBarView.titleLabel.text = model.result.shop.shopName;
        [self.tableview reloadData];
        [self endRefresh];
        [SVProgressHUD dismiss];

    } failBlock:^(NSError *error) {
        [self endRefresh];
        [SVProgressHUD dismiss];

    }];
}
-(void)endRefresh{
    [self.tableview.mj_footer endRefreshing];
}
-(void)rightItemClick{
    self.shareBottomView = [[[NSBundle mainBundle] loadNibNamed:@"NRWCarShareBottomView" owner:self options:nil]lastObject];
    kSelfWeak;
    ShareCars *carModel = self.carLists[0];
    NSString *shareUrl = [NSString stringWithFormat:@"%@%@",ImageApi,self.shareModel.result.shareFileUrl];
    self.shareBottomView.shareBlock = ^(NSString *content, NSInteger type) {
        if (type == 2 ) {
            [NRWCarShareTool shareCarDetailWithCarDetailUrl:shareUrl imageUrl:carModel.carImageUrl customerTitle:content shopName:weakSelf.shareModel.result.shop.shopName shareType:ShareTypeWeiXin];
        }else if (type == 1){
            [NRWCarShareTool shareCarDetailWithCarDetailUrl:shareUrl imageUrl:carModel.carImageUrl customerTitle:content shopName:weakSelf.shareModel.result.shop.shopName shareType:ShareTypeWXCircle];
        }
    };
    self.shareBottomView.imageUrl = carModel.carImageUrl;
    self.shareBottomView.contentF.text = @"好车超低价，质量过关，放心买！";
    self.shareBottomView.frame = self.view.frame;
    [self.view addSubview:self.shareBottomView];
    [self.shareBottomView show];
}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    
}
-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
